
// 200.岛屿数量
class Solution {
public:
    int numIslands(vector<vector<char>>& grid) {
        // 直接通过DFS来完成
        int n = grid.size() , m = grid[0].size();
        int dx[] = {0 , 0, -1 ,1};
        int dy[] = {1 , -1, 0 ,0};

        function<void(int x , int y)> dfs = [&](int x , int y)
        {
            grid[x][y] = '0';
            for(int k = 0 ; k < 4 ;k++)
            {
                int a = x + dx[k] , b = y + dy[k];
                if(a >=0 && a < n && b >=0 && b< m && grid[a][b] == '1') dfs(a ,b);
            }
        };

        int ret = 0;
        for(int i = 0 ; i < n ; i++)
        {
            for(int j = 0 ;j < m ; j++)
            {
                if(grid[i][j] == '1' ) 
                {
                    ret++;
                    dfs(i , j);
                }
            }
        }
        return ret;
    }
};